package vn.bus.bk.place;

import java.util.ArrayList;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.MenuItem;

import vn.bus.bk.R;
import vn.bus.bk.adapter.ListItemPlaceAdapter;
import vn.bus.bk.object.ItemPlaceInfo;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class SearchToPlace extends SherlockFragmentActivity implements OnItemClickListener {

    private ListView listPlace;
    private ArrayList<ItemPlaceInfo> listItemPlace;
    private ListItemPlaceAdapter adapter;
    private String[] listTypePlace;
    private String[] listTypePlaceInDB;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	// TODO Auto-generated method stub
	super.onCreate(savedInstanceState);
	setContentView(R.layout.searchtoplace);
	setTitle(getString(R.string.menu_item_4));
	getSupportActionBar().setDisplayHomeAsUpEnabled(true);
	getSupportActionBar().setHomeButtonEnabled(true);
	listTypePlace = getResources().getStringArray(R.array.arrayTypePlace);
	listTypePlaceInDB = getResources().getStringArray(R.array.arrayTypePlaceInDB);
	init();
	createArrayList();
	setup();
    }

    private void init() {
	// TODO Auto-generated method stub
	listPlace = (ListView) findViewById(R.id.listTypePlace);
	listPlace.setOnItemClickListener(this);
    }
    
    private void createArrayList(){
	listItemPlace = new ArrayList<ItemPlaceInfo>();
	listItemPlace.add(new ItemPlaceInfo(createBitmapFromId(R.drawable.university), listTypePlace[0]));
	listItemPlace.add(new ItemPlaceInfo(createBitmapFromId(R.drawable.park), listTypePlace[1]));
	listItemPlace.add(new ItemPlaceInfo(createBitmapFromId(R.drawable.market), listTypePlace[2]));
	listItemPlace.add(new ItemPlaceInfo(createBitmapFromId(R.drawable.cinema), listTypePlace[3]));
	listItemPlace.add(new ItemPlaceInfo(createBitmapFromId(R.drawable.station),listTypePlace[4]));
	listItemPlace.add(new ItemPlaceInfo(createBitmapFromId(R.drawable.hospital), listTypePlace[5]));
	listItemPlace.add(new ItemPlaceInfo(createBitmapFromId(R.drawable.museum),listTypePlace[6]));
	listItemPlace.add(new ItemPlaceInfo(createBitmapFromId(R.drawable.travelplace), listTypePlace[7]));
    }

    private void setup() {
	// TODO Auto-generated method stub
	adapter = new ListItemPlaceAdapter(SearchToPlace.this, listItemPlace);
	listPlace.setAdapter(adapter);
    }
    
    private Bitmap createBitmapFromId(int id){
	return BitmapFactory.decodeResource(getResources(), id);
    }

    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
	// TODO Auto-generated method stub
	String type = listTypePlaceInDB[position];
	ListPlaceForAType.typePlace = type;
	startActivity(new Intent(SearchToPlace.this, ListPlaceForAType.class));
	overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
	if (item.getItemId() == android.R.id.home) {
	    finish();
	}
	return super.onOptionsItemSelected(item);
    }
    
    

}
